from time import sleep

from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains


class Base():

    def __init__(self, driver):
        self.driver = driver
        self.timeout = 30
        self.poll = 0.5

    def findelementnew(self, loctor):
        # 定位到元素，返回元素对象，没定位到元素，返回timeout异常
        ele = WebDriverWait(self.driver, self.timeout, self.poll).until(EC.presence_of_element_located(loctor))
        return ele

    def findelement(self, loctor):
        # args:     loctor 传元祖，如（"id","xx"）
        try:
            element = WebDriverWait(self.driver, self.timeout, self.poll).until(lambda x: x.find_element(*loctor))
            return element
        except:
            return []

    def findelements(self, loctor):
        try:
            element = WebDriverWait(self.driver, self.timeout, self.poll).until(lambda x: x.find_elements(*loctor))
            return element
        except:
            return []

    # 封装click方法
    def click(self, loctor):
        ele = self.findelement(loctor)
        ele.click()

    # 封装sendkeys方法
    def sendKeys(self, loctor, text, is_clear_first=False):
        ele = self.findelement(loctor)
        if is_clear_first:
            ele.clear()
        ele.send_keys(text)
        sleep(1)

    # 封装clear方法
    def clear(self, loctor):
        ele = self.findelement(loctor)
        ele.clear()

    def isElementExist(self, loctor):
        try:
            ele = self.findelement(loctor)
            return True
        except:
            return False

    def isElementExist2(self, loctor):
        ele = self.findelements(loctor)
        n = len(ele)
        if n == 0:
            return False
        elif n == 1:
            return True
        else:
            return True

    def isDisplayed(self, loctor):
        ele = self.findelement(loctor)
        r = ele.is_displayed()
        return r

    def isSelected(self, loctor):
        ele = self.findelement(loctor)
        r = ele.is_selected()
        return r

    def isTitle(self, _title):
        # 返回bool值
        try:
            result = WebDriverWait(self.driver, self.timeout, self.poll).until(EC.title_is(_title))
            return result
        except:
            return False

    def is_title_containa(self, _title):
        # 返回bool值
        try:
            result = WebDriverWait(self.driver, self.timeout, self.poll).until(EC.title_contains(_title))
            return result
        except:
            return False

    def is_text_in_element(self, loctor, _text):
        # 返回bool值
        try:
            result = WebDriverWait(self.driver, self.timeout, self.poll).until(
                EC.text_to_be_present_in_element(loctor, _text))
            return result
        except:
            return False

    def is_value_in_element(self, loctor, _value):
        # 返回bool值
        try:
            result = WebDriverWait(self.driver, self.timeout, self.poll).until(
                EC.text_to_be_present_in_element_value(loctor, _value))
            return result
        except:
            return False

    def is_alert(self):
        # 找到alert，返回alert对象，没有则返回False
        try:
            result = WebDriverWait(self.driver, self.timeout, self.poll).until(
                EC.alert_is_present())
            return result
        except:
            return False

    # 鼠标悬停操作
    def move_to_element(self, loctor):
        ele = self.findelement(loctor)
        ActionChains(driver).move_to_element(ele).perform()

    # 通过索引，index是索引第几个，从0 开始，默认第一个
    def select_by_index(self, loctor, index=0):
        ele = self.findelement(loctor)
        Select(ele).select_by_index(index)

    # 通过value
    def select_by_value(self, loctor, value):
        ele = self.findelement(loctor)
        Select(ele).select_by_value(value)

    # 通过文本值定位
    def select_by_text(self, loctor, text):
        ele = self.findelement(loctor)
        Select(ele).select_by_visible_text(text)


    # 滚动到底部
    def js_scroll_to_button(self, x=0):
        js = "window.scrollTo(%s, document.body.scrollHeight)"%x
        self.driver.execute_script(js)

     # 滚动到顶部
    def js_scroll_to_top(self):
        js = "window.scrollTo(0, 0)"
        self.driver.execute_script(js)

    # 聚焦元素
    def js_focus_element(self, loctor):
        target = self.findelement(loctor)
        self.driver.execute_script("arguments[0].scrollIntoView();", target)




if __name__ == "__main__":
    driver = webdriver.Chrome()
    url = "http://52.80.159.11:9510/formLogin"
    driver.get(url)
    yurongtong = Base(driver)

    loc1 = ('xpath', '//*[@id="appWrap"]/div[2]/div/div/div/div[2]/div/div[1]/div[2]/div[1]')
    loc2 = ('id', 'phone')
    loc3 = ('id', 'code')
    loc4 = ('class name', 'components-front-login-LoginBox-Btn-__index__button--3StYq')
    loc5 = (By.CLASS_NAME, 'components-front-login-LoginBox-Btn-__index__button')

    # yurongtong.click(loc1)
    # yurongtong.sendKeys(loc2, '15823290852')
    # yurongtong.clear(loc2)
    # yurongtong.sendKeys(loc3, '111111')
    # yurongtong.click(loc4)
    # ele1 = yurongtong.isElementExist2(loc1)
    # ele2 = yurongtong.isElementExist2(loc5)
    # print(ele1)
    # print(ele2)

    a = EC.title_is("誉存普惠服务平台")(driver)

    print(a)
